package com.we.risk.phoneBook.adm;

import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

public class userBasicInfo {
    public HashMap<String, Integer> provMap;
    public HashMap<String, Integer> cityMap;
    public HashMap<String, Integer> phn3Map;

    public int adb_cnt;
    public int adb_fmy_cnt;
    public int adb_lon_cnt;
    public int adb_good1_cnt;
    public int adb_good2_cnt;
    public int adb_bad_cnt;
    public int adb_phn_cnt;
    public int adb_tel_400_cnt;
    public int adb_tel_9596_cnt;
    public int adb_tel_cnt;
    public int adb_tel_spc_cnt;

    public HashSet<String> nameSet;
    public int adb_nam_all_cnt; // 去重 rte,max,min,avg
    public int adb_nam_len_min = Integer.MAX_VALUE;
    public int adb_nam_len_max = Integer.MIN_VALUE;
    public int adb_nam_len_sum;

    public int adb_zh_num_cnt;
    public int adb_zh_num_min = Integer.MAX_VALUE;
    public int adb_zh_num_max = Integer.MIN_VALUE;
    public int adb_zh_num_sum;

    public int adb_en_num_cnt;
    public int adb_en_num_min = Integer.MAX_VALUE;
    public int adb_en_num_max = Integer.MIN_VALUE;
    public int adb_en_num_sum;

    public int adb_num_num_cnt;
    public int adb_num_num_min = Integer.MAX_VALUE;
    public int adb_num_num_max = Integer.MIN_VALUE;
    public int adb_num_num_sum;

    // 手机号数
    public int adb_pho_sam_prv_cnt;
    public int adb_pho_sam_cty_cnt;
    public int adb_pid_sam_prv_cnt;
    public int adb_pid_sam_cty_cnt;
    public int adb_add_sam_prv_cnt;
    public int adb_add_sam_cty_cnt;
    public int adb_pho_sam_opt_cnt;
    public int adb_opt_lt_cnt;
    public int adb_opt_yd_cnt;
    public int adb_opt_dx_cnt;
    public int adb_opt_other_cnt;

    public userBasicInfo() {
        provMap = new HashMap<>();
        cityMap = new HashMap<>();
        phn3Map = new HashMap<>();
        nameSet = new HashSet<>();
    }

    public void process(phoneBookObject book) {
        String prov = book.getMob_prov();
        if (prov != null) {
            provMap.put(prov, provMap.getOrDefault(prov, 0) + 1);
        }

        String city = book.getMob_city();
        if (city != null) {
            cityMap.put(city, cityMap.getOrDefault(city, 0) + 1);
        }

        String phn3 = book.getPhn3();
        if (phn3 != null) {
            phn3Map.put(phn3, phn3Map.getOrDefault(phn3, 0) + 1);
        }

        adb_cnt += 1;

        if (book.isFamTag()) {
            adb_fmy_cnt += 1;
        }

        if (book.isLonTag()) {
            adb_lon_cnt += 1;
        }

        if (book.isGood1Tag()) {
            adb_good1_cnt += 1;
        }

        if (book.isGood2Tag()) {
            adb_good2_cnt += 1;
        }

        if (book.isBadTag()) {
            adb_bad_cnt += 1;
        }

        if (book.getRegBokPhn()) {
            adb_phn_cnt += 1;
        }

        if (book.getIs400()) {
            adb_tel_400_cnt += 1;
        }

        if (book.getIs9596()) {
            adb_tel_9596_cnt += 1;
        }

        if (book.getIsTel()) {
            adb_tel_cnt += 1;
        }

        if (book.getIsSpc()) {
            adb_tel_spc_cnt += 1;
        }
        // 省市一致信息
        if (book.isPhoIsSamPrv()) {
            adb_pho_sam_prv_cnt += 1;
        }

        if (book.isPhoIsSamCty()) {
            adb_pho_sam_cty_cnt += 1;
        }

        if (book.isPhoIsSamOpt()) {
            adb_pho_sam_opt_cnt += 1;
        }

        if (book.isPidIsSamPrv()) {
            adb_pid_sam_prv_cnt += 1;
        }

        if (book.isPidIsSamCty()) {
            adb_pid_sam_cty_cnt += 1;
        }

        if (book.isAddIsSamPrv()) {
            adb_add_sam_prv_cnt += 1;
        }

        if (book.isAddIsSamCty()) {
            adb_add_sam_cty_cnt += 1;
        }
        // 运营商
        if (book.getPhoOptIsyd()) {
            adb_opt_yd_cnt += 1;
        }

        if (book.getPhoOptIslt()) {
            adb_opt_lt_cnt += 1;
        }

        if (book.getPhoOptIsdx()) {
            adb_opt_dx_cnt += 1;
        }

        if (book.getPhoOptIsOther()) {
            adb_opt_other_cnt += 1;
        }
        // name类
        String name = book.getName();
        if (name != null) {
            // 去重cnt
            nameSet.add(name);
            int nameLen = book.getNameLen();
            adb_nam_all_cnt += 1;
            adb_nam_len_min = (adb_nam_len_min < nameLen ? adb_nam_len_min : nameLen);
            adb_nam_len_max = (adb_nam_len_max > nameLen ? adb_nam_len_max : nameLen);
            //            System.out.println(name + "," + nameLen);
            adb_nam_len_sum += nameLen;

            int zhNum = book.getZhNum();
            adb_zh_num_cnt += 1;
            adb_zh_num_min = (adb_zh_num_min < zhNum ? adb_zh_num_min : zhNum);
            adb_zh_num_max = (adb_zh_num_max > zhNum ? adb_zh_num_max : zhNum);
            adb_zh_num_sum += zhNum;

            int enNum = book.getEnNum();
            adb_en_num_cnt += 1;
            adb_en_num_min = (adb_en_num_min < enNum ? adb_en_num_min : enNum);
            adb_en_num_max = (adb_en_num_max > enNum ? adb_en_num_max : enNum);
            adb_en_num_sum += enNum;

            int numNum = book.getNumNum();
            adb_num_num_cnt += 1;
            adb_num_num_min = (adb_num_num_min < numNum ? adb_num_num_min : numNum);
            adb_num_num_max = (adb_num_num_max > numNum ? adb_num_num_max : numNum);
            adb_num_num_sum += numNum;
        }
    }

    public void format(JSONObject resJson) {
        if (adb_cnt > 0) {
            formatProv(resJson);
            formatCity(resJson);
            formatPhn3(resJson);

            resJson.put("adb_cnt", adb_cnt);
            resJson.put("adb_fmy_cnt", adb_fmy_cnt);
            resJson.put("adb_fmy_rte", BigDecimal.valueOf(adb_fmy_cnt / (double) adb_cnt));
            resJson.put("adb_lon_cnt", adb_lon_cnt);
            resJson.put("adb_lon_rte", BigDecimal.valueOf(adb_lon_cnt / (double) adb_cnt));
            resJson.put("adb_good1_cnt", adb_good1_cnt);
            resJson.put("adb_good1_rte", BigDecimal.valueOf(adb_good1_cnt / (double) adb_cnt));
            resJson.put("adb_good2_cnt", adb_good2_cnt);
            resJson.put("adb_good2_rte", BigDecimal.valueOf(adb_good2_cnt / (double) adb_cnt));
            resJson.put("adb_bad_cnt", adb_bad_cnt);
            resJson.put("adb_bad_rte", BigDecimal.valueOf(adb_bad_cnt / (double) adb_cnt));
            resJson.put("adb_phn_cnt", adb_phn_cnt); // 手机号数
            resJson.put("adb_phn_rte", BigDecimal.valueOf(adb_phn_cnt / (double) adb_cnt));
            resJson.put("adb_tel_400_cnt", adb_tel_400_cnt);
            resJson.put("adb_tel_400_rte", BigDecimal.valueOf(adb_tel_400_cnt / (double) adb_cnt));
            resJson.put("adb_tel_9596_cnt", adb_tel_9596_cnt);
            resJson.put(
                    "adb_tel_9596_rte", BigDecimal.valueOf(adb_tel_9596_cnt / (double) adb_cnt));
            resJson.put("adb_tel_cnt", adb_tel_cnt);
            resJson.put("adb_tel_rte", BigDecimal.valueOf(adb_tel_cnt / (double) adb_cnt));
            resJson.put("adb_tel_spc_cnt", adb_tel_spc_cnt);
            resJson.put("adb_tel_spc_rte", BigDecimal.valueOf(adb_tel_spc_cnt / (double) adb_cnt));

            resJson.put("adb_nam_cnt", nameSet.size());
            resJson.put("adb_nam_rte", BigDecimal.valueOf(nameSet.size() / (double) adb_cnt));
            resJson.put("adb_nam_len_min", adb_nam_len_min);
            resJson.put("adb_nam_len_max", adb_nam_len_max);
            resJson.put(
                    "adb_nam_len_avg",
                    BigDecimal.valueOf(adb_nam_len_sum / (double) adb_nam_all_cnt));
            resJson.put("adb_zh_num_cnt", adb_zh_num_cnt);
            resJson.put("adb_zh_num_rte", BigDecimal.valueOf(adb_zh_num_cnt / (double) adb_cnt));
            resJson.put("adb_zh_num_min", adb_zh_num_min);
            resJson.put("adb_zh_num_max", adb_zh_num_max);
            resJson.put(
                    "adb_zh_num_avg", BigDecimal.valueOf(adb_zh_num_sum / (double) adb_zh_num_cnt));
            resJson.put("adb_en_num_cnt", adb_en_num_cnt);
            resJson.put("adb_en_num_rte", BigDecimal.valueOf(adb_en_num_cnt / (double) adb_cnt));
            resJson.put("adb_en_num_min", adb_en_num_min);
            resJson.put("adb_en_num_max", adb_en_num_max);
            resJson.put(
                    "adb_en_num_avg", BigDecimal.valueOf(adb_en_num_sum / (double) adb_en_num_cnt));
            resJson.put("adb_num_num_cnt", adb_num_num_cnt);
            resJson.put("adb_num_num_rte", BigDecimal.valueOf(adb_num_num_cnt / (double) adb_cnt));
            resJson.put("adb_num_num_min", adb_num_num_min);
            resJson.put("adb_num_num_max", adb_num_num_max);
            resJson.put(
                    "adb_num_num_avg",
                    BigDecimal.valueOf(adb_num_num_sum / (double) adb_num_num_cnt));

            resJson.put("adb_pho_sam_prv_cnt", adb_pho_sam_prv_cnt);
            resJson.put("adb_pho_sam_cty_cnt", adb_pho_sam_cty_cnt);
            resJson.put("adb_pho_sam_opt_cnt", adb_pho_sam_opt_cnt);
            resJson.put("adb_pid_sam_prv_cnt", adb_pid_sam_prv_cnt);
            resJson.put("adb_pid_sam_cty_cnt", adb_pid_sam_cty_cnt);
            resJson.put("adb_add_sam_prv_cnt", adb_add_sam_prv_cnt);
            resJson.put("adb_add_sam_cty_cnt", adb_add_sam_cty_cnt);
            resJson.put("adb_opt_lt_cnt", adb_opt_lt_cnt);
            resJson.put("adb_opt_yd_cnt", adb_opt_yd_cnt);
            resJson.put("adb_opt_dx_cnt", adb_opt_dx_cnt);
            resJson.put("adb_opt_other_cnt", adb_opt_other_cnt);
            if (adb_phn_cnt > 0) {
                resJson.put(
                        "adb_pho_sam_prv_rte",
                        BigDecimal.valueOf(adb_pho_sam_prv_cnt / (double) adb_phn_cnt));
                resJson.put(
                        "adb_pho_sam_cty_rte",
                        BigDecimal.valueOf(adb_pho_sam_cty_cnt / (double) adb_phn_cnt));
                resJson.put(
                        "adb_pho_sam_opt_rte",
                        BigDecimal.valueOf(adb_pho_sam_opt_cnt / (double) adb_phn_cnt));
                resJson.put(
                        "adb_pid_sam_prv_rte",
                        BigDecimal.valueOf(adb_pid_sam_prv_cnt / (double) adb_phn_cnt));
                resJson.put(
                        "adb_pid_sam_cty_rte",
                        BigDecimal.valueOf(adb_pid_sam_cty_cnt / (double) adb_phn_cnt));
                resJson.put(
                        "adb_add_sam_prv_rte",
                        BigDecimal.valueOf(adb_add_sam_prv_cnt / (double) adb_phn_cnt));
                resJson.put(
                        "adb_add_sam_cty_rte",
                        BigDecimal.valueOf(adb_add_sam_cty_cnt / (double) adb_phn_cnt));

                resJson.put(
                        "adb_opt_lt_rte",
                        BigDecimal.valueOf(adb_opt_lt_cnt / (double) adb_phn_cnt));
                resJson.put(
                        "adb_opt_yd_rte",
                        BigDecimal.valueOf(adb_opt_yd_cnt / (double) adb_phn_cnt));
                resJson.put(
                        "adb_opt_dx_rte",
                        BigDecimal.valueOf(adb_opt_dx_cnt / (double) adb_phn_cnt));
                resJson.put(
                        "adb_opt_other_rte",
                        BigDecimal.valueOf(adb_opt_other_cnt / (double) adb_phn_cnt));
            }
        }
    }

    private void formatPhn3(JSONObject resJson) {
        int adb_phn3_num = phn3Map.size();
        int adb_phn3_cnt_min = Integer.MAX_VALUE;
        int adb_phn3_cnt_max = Integer.MIN_VALUE;
        int adb_phn3_cnt_sum = 0;

        if (adb_phn3_num > 0) {
            Iterator<String> phn3Iterator = phn3Map.keySet().iterator();
            while (phn3Iterator.hasNext()) {
                String phn3 = phn3Iterator.next();
                Integer num = phn3Map.get(phn3);

                adb_phn3_cnt_sum += num;
                adb_phn3_cnt_min = (adb_phn3_cnt_min < num ? adb_phn3_cnt_min : num);
                adb_phn3_cnt_max = (adb_phn3_cnt_max > num ? adb_phn3_cnt_max : num);
            }

            resJson.put("adb_phn3_num", adb_phn3_num);
            resJson.put("adb_phn3_cnt_min", adb_phn3_cnt_min);
            resJson.put("adb_phn3_cnt_max", adb_phn3_cnt_max);
            resJson.put(
                    "adb_phn3_cnt_avg",
                    BigDecimal.valueOf(adb_phn3_cnt_sum / (double) adb_phn3_num));
        }
    }

    private void formatCity(JSONObject resJson) {
        int adb_cty_num = cityMap.size();
        int adb_bok_phn_cty_cnt_min = Integer.MAX_VALUE;
        int adb_bok_phn_cty_cnt_max = Integer.MIN_VALUE;
        int adb_bok_phn_cty_cnt_sum = 0;

        if (adb_cty_num > 0) {
            Iterator<String> ctyIterator = cityMap.keySet().iterator();
            while (ctyIterator.hasNext()) {
                String city = ctyIterator.next();
                Integer num = cityMap.get(city);

                adb_bok_phn_cty_cnt_sum += num;
                adb_bok_phn_cty_cnt_min =
                        (adb_bok_phn_cty_cnt_min < num ? adb_bok_phn_cty_cnt_min : num);
                adb_bok_phn_cty_cnt_max =
                        (adb_bok_phn_cty_cnt_max > num ? adb_bok_phn_cty_cnt_max : num);
            }

            resJson.put("adb_cty_num", adb_cty_num);
            resJson.put("adb_bok_phn_cty_cnt_min", adb_bok_phn_cty_cnt_min);
            resJson.put("adb_bok_phn_cty_cnt_max", adb_bok_phn_cty_cnt_max);
            resJson.put(
                    "adb_bok_phn_cty_cnt_avg",
                    BigDecimal.valueOf(adb_bok_phn_cty_cnt_sum / (double) adb_cty_num));
        }
    }

    private void formatProv(JSONObject resJson) {
        int adb_prv_num = provMap.size();
        int adb_bok_phn_prv_cnt_min = Integer.MAX_VALUE;
        int adb_bok_phn_prv_cnt_max = Integer.MIN_VALUE;
        int adb_bok_phn_prv_cnt_sum = 0;

        if (adb_prv_num > 0) {
            Iterator<String> provIterator = provMap.keySet().iterator();
            while (provIterator.hasNext()) {
                String prov = provIterator.next();
                Integer num = provMap.get(prov);

                adb_bok_phn_prv_cnt_sum += num;
                adb_bok_phn_prv_cnt_min =
                        (adb_bok_phn_prv_cnt_min < num ? adb_bok_phn_prv_cnt_min : num);
                adb_bok_phn_prv_cnt_max =
                        (adb_bok_phn_prv_cnt_max > num ? adb_bok_phn_prv_cnt_max : num);
            }

            resJson.put("adb_prv_num", adb_prv_num);
            resJson.put("adb_bok_phn_prv_cnt_min", adb_bok_phn_prv_cnt_min);
            resJson.put("adb_bok_phn_prv_cnt_max", adb_bok_phn_prv_cnt_max);
            resJson.put(
                    "adb_bok_phn_prv_cnt_avg",
                    BigDecimal.valueOf(adb_bok_phn_prv_cnt_sum / (double) adb_prv_num));
        }
    }
}
